﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Empresa.Sistema.Datos.Entidades;
using Empresa.Sistema.Aplicacion;

namespace Empresa.Sistema.Presentacion
{
    public partial class mntCargos : Form
    {


        #region "Instanciar"

        private static mntCargos m_FormDefInstance;
        public static mntCargos DefInstance
        {
            get
            {
                if (m_FormDefInstance == null || m_FormDefInstance.IsDisposed)
                    m_FormDefInstance = new mntCargos();
                return m_FormDefInstance;
            }
            set
            {
                m_FormDefInstance = value;
            }
        }

        #endregion 

        public mntCargos()
        {
            InitializeComponent();
        }

        List<Cargo> lista = new List<Cargo>();
        public Cargo car;
        public string accion;

        private void mntCargos_Load(object sender, EventArgs e)
        {
            lista = new Cargos().Listar();
            dgvCargos.AutoGenerateColumns = false;
            dgvCargos.DataSource = lista;
        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
                accion = "adicionar";
                var regCargos = new regCargos();
                regCargos.Owner = this;
                regCargos.ShowDialog();
                mntCargos_Load(new object(), new EventArgs());
        }

        private void btnModificar_Click(object sender, EventArgs e)
        {
            if (dgvCargos.CurrentCell.RowIndex < 0)
            {
                MessageBox.Show("Seleccione una fila");
            }
            else
            {
                accion = "modificar";
                var regCargos = new regCargos();
                regCargos.Owner = this;
                regCargos.ShowDialog();
                //btnBuscar_Click(new object(), new EventArgs());
                mntCargos_Load(new object(), new EventArgs());
            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            if (dgvCargos.CurrentCell.RowIndex < 0)
            {
                MessageBox.Show("Seleccione una fila");
            }
            else
            {
                accion = "eliminar";
                var regCargos = new regCargos();
                regCargos.Owner = this;
                regCargos.ShowDialog();
                mntCargos_Load(new object(), new EventArgs());
            }
        }

        private void txtCargo_TextChanged(object sender, EventArgs e)
        {
            lista = new Cargos().ConsultaPorDescripcion(txtCargo.Text);
            dgvCargos.AutoGenerateColumns = false;
            dgvCargos.DataSource = lista;
        }

        private void dgvCargos_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvCargos.CurrentCell.RowIndex >= 0)
            {
                car = (Cargo)lista[dgvCargos.CurrentCell.RowIndex];
            }
        }
    }
}
